<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePermissionTables extends Migration
{
    const USER_ROLES = 'admin_user_roles';
    const ROLES = 'admin_roles';
    const ROLE_PERMISSIONS = 'admin_role_permissions';
    const PERMISSIONS = 'admin_permissions';

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // 角色表
        Schema::create(self::ROLES, function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 30)->default('');
            $table->string('description', 100)->default('');
            $table->timestamps();
        });

        // 权限表
        Schema::create(self::PERMISSIONS, function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 30)->default('');
            $table->string('description', 100)->default('');
            $table->timestamps();
        });

        // 用户角色表
        Schema::create(self::USER_ROLES, function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->integer('role_id');
            $table->timestamps();
        });

        // 角色权限表
        Schema::create(self::ROLE_PERMISSIONS, function (Blueprint $table) {
            $table->increments('id');
            $table->integer('role_id');
            $table->integer('permission_id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists(self::USER_ROLES);
        Schema::dropIfExists(self::ROLES);
        Schema::dropIfExists(self::ROLE_PERMISSIONS);
        Schema::dropIfExists(self::PERMISSIONS);
    }
}
